package figure8;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;

public class MIDIRecorder implements Receiver
{
	private javax.sound.midi.Track track;
	int i; 
	long time;
    Receiver synthRcvr;

	public MIDIRecorder(javax.sound.midi.Track tracky, Receiver synthReceiver){
		track = tracky;
		i = 0;
		synthRcvr = synthReceiver;
	}

	public void close() {
		synthRcvr.close();
	}

	public void send(MidiMessage message, long lTimeStamp) {
		if (message instanceof ShortMessage) {
		    i++;
		    synthRcvr.send(message, -1);
			track.add(new MidiEvent(message, (long) (2*(System.currentTimeMillis() - time))));
		}
	}
	
	public void reset (javax.sound.midi.Track tracky) {
		track = tracky;
		time = System.currentTimeMillis();
	}
}

